<?php
include 'test_header.php';

//建立测试用的扩展类		
class test extends db_obj{

	function __construct( $db ){
		//传入表的名字及数据库连接
		parent::__construct( $db , 'test' );
		//设置自己的属性和默认值
		$this->add( 'k' , 'key' );
		$this->add( 'v' , 'value');
	}		
}

class test_abstract_class_db_obj extends UnitTestCase{

	function setUp(){
		//生成一个测试用的表
		$db = db_connect();	
		$db->query("CREATE TABLE `test` ( `k` varchar(255) , `v` varchar(255) , PRIMARY KEY (`k`))");
	}
	function tearDown(){

		$db = db_connect();
		$db->query('DROP TABLE `test`');
	}

	//插入数据库以及
	function test_funtions_save_and_load(){

		$test = new test( db_connect() );
		$test->set( 'k' , '123456' );
		$test->set( 'v' , '789101' );
		$this->assertEqual( '123456' , $test->get('k') ) ;
		$this->assertEqual( '789101' , $test->get('v') ) ;

		$this->assertTrue($test->save());
		$this->assertTrue($test->load('k','123456'));

		$this->assertEqual( '123456' , $test->get('k') ) ;
		$this->assertEqual( '789101' , $test->get('v') ) ;
	}

	function test_functoin_load_if_no_param_send_in_will_throw_exception(){
		//应该保证load()返回的结果唯一性
		//断言load()的两个参数,如果有没有正确给出
		//的话则期待一个异常
	
		$test = new test( db_connect() );
		$this->expectException();
		$test->load();
	}


	function test_function_delete(){

		$test = new test( db_connect() );
		//先增加一行,在删除之,断言此行不存在
		$test->set( 'k' , '666' );
		$test->set( 'v' , '777' );
		
		$this->assertTrue( $test->save() );
		
		$this->assertTrue( $test->load('k' , '666') );

		$this->assertTrue( $test->delete('k' , '666') ); 

		$this->expectException();
		$test->load( 'k' , '666' );
	}
	
	

 
}	

$test = new test_abstract_class_db_obj( );
$test->run( new HtmlReporter() );

